<?php

namespace app\api\model;

use think\Model;

class RtcPlay extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    /**
     * Notes:创建频道插入频道成员
     * User: tangfen
     * Date: 2023/12/27
     * Time: 15:05
     * $type 1=群主(创建时加入)，2=群成员
     * @param $params
     */
    public function insertRtcPlay($data, $type = '')
    {
        //创建者也算频道成员
        $RtcPlay = $this::where(['channel_id' => $data['channel_id'], 'uid' => $data['uid']])->value('id');
        if (empty($RtcPlay)) {
            if ($type == 1) {//群主
                $res = $this::insert(
                    [
                        'rtc_anchor_id' => $data['rtc_anchor_id'],
                        'uid' => $data['uid'],
                        'channel_id' => $data['channel_id'],
                        'is_homeowner' => 1,//是否群主 1=是，2=否
                        'is_full_member' => 1,
                        'create_time' => time(),
                        'update_time' => time()
                    ]
                );
            } else {//群成员加人

                $where = [
                    'ra.channel_id' => $data['channel_id'],
                ];
                $res = RtcAnchor::alias('ra')
                    ->join('im_group g', 'ra.im_group_id=g.id', 'RIGHT')
                    ->where($where)
                    ->field('ra.id as rtc_anchor_id,ra.num')
                    ->find();

                $channelNum = RtcPlay::where(['status' => 1, 'channel_id' => $data['channel_id']])->count();
                //判断正式成员数量
                if ($res['num'] > $channelNum) {
                    $is_full_member = 1;//正式
                } else {
                    $is_full_member = 2;
                }
                $res = $this::insert(
                    [
                        'rtc_anchor_id' => $res['rtc_anchor_id'],
                        'uid' => $data['uid'],
                        'channel_id' => $data['channel_id'],
                        'is_homeowner' => 2,//是否群主 2否
                        'is_full_member' => $is_full_member,
                        'create_time' => time(),
                        'update_time' => time()
                    ]
                );
            }
            if ($res) {
                return true;
            } else {
                return_ajax(0, '加入频道用户入库失败');
            }
        } else {
            return_ajax(0, '该用户已加入该频道');
        }
    }


    /**
     * Notes:获取正式成员列表
     * User: tangfen
     * Date: 2023/12/28
     * Time: 17:27
     * @param $params
     * @return RtcPlay|\think\Paginator
     */
    public function rtcPlayFormalList($params)
    {
        $where = [
            'rp.rtc_anchor_id' => $params['id'],
            'rp.is_full_member'=>1//正式成员=1，非正式成员=2
        ];
        $list = $this::alias('rp')
            ->join('user u', 'u.id=rp.uid', 'LEFT')
            ->where($where)
            ->field('rp.*,u.id as uid ,u.nick,u.portrait,u.username')
            ->order('rp.create_time asc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return $list;
    }





}